package com.apex.shared.starter.utils;

import com.apex.shared.starter.constant.HttpStatusCode;
import com.apex.shared.starter.exception.ServiceException;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.util.Collection;

/**
 * 断言工具类
 *
 * @author MoNaiHui
 */
public class Asserts {

    /**
     * 断言对象不为空
     */
    public static void notNull(Object object, String message) {
        if (ObjectUtils.isEmpty(object)) {
            fail(message);
        }
    }

    /**
     * 断言对象为空
     */
    public static void isNull(Object object, String message) {
        if (!ObjectUtils.isEmpty(object)) {
            fail(message);
        }
    }

    /**
     * 断言字符串不为空
     */
    public static void notEmpty(String str, String message) {
        if (!StringUtils.hasText(str)) {
            fail(message);
        }
    }

    /**
     * 断言集合不为空
     */
    public static void notEmpty(Collection<?> collection, String message) {
        if (CollectionUtils.isEmpty(collection)) {
            fail(message);
        }
    }

    /**
     * 断言表达式为真
     */
    public static void isTrue(boolean expression, String message) {
        if (!expression) {
            fail(message);
        }
    }

    /**
     * 断言表达式为假
     */
    public static void isFalse(boolean expression, String message) {
        if (expression) {
            fail(message);
        }
    }

    /**
     * 断言对象相等
     */
    public static void equals(Object expected, Object actual, String message) {
        if (!ObjectUtils.nullSafeEquals(expected, actual)) {
            fail(message);
        }
    }

    /**
     * 断言对象不相等
     */
    public static void notEquals(Object expected, Object actual, String message) {
        if (ObjectUtils.nullSafeEquals(expected, actual)) {
            fail(message);
        }
    }

    /**
     * 直接抛出业务异常
     */
    public static void fail(String message) {
        throw new ServiceException(message);
    }

    /**
     * 直接抛出业务异常
     */
    public static void fail(HttpStatusCode httpStatusCode) {
        throw new ServiceException(httpStatusCode);
    }

    /**
     * 直接抛出业务异常
     */
    public static void fail(Integer code, String message) {
        throw new ServiceException(code, message);
    }
} 